const { 
  getList, 
  getDetail, 
  newBlog,
  updateBlog,
  delBlog
} = require('../controller/blog');
const { SuccessModel, ErrorModel } = require('../model/resModel');

// 统一的登录验证函数
const loginCheck = (req) => {
  if(!req.session.username) {   
    return Promise.resolve(   // 注意：是Promise.resolve
      new ErrorModel('尚未登录')
    )
  } 
}

const handleBlogRouter = (req, res) => {
  const method = req.method;
  const id = req.query.id;

  // 获取博客列表
  if(method == 'GET' && req.path == '/api/blog/list') {
    let author = req.query.author || '';
    const keyword = req.query.keyword || '';

    if(req.query.isadmin) {
      // 管理员界面
      const loginCheckResult = loginCheck(req)
      if(loginCheckResult) {
        // 未登录
        return loginCheckResult
      }
      // 强制查询自己的博客
      author = req.session.username
    }

    const result = getList(author, keyword);
    return result.then(listData => {
      return new SuccessModel(listData);
    })
  }

  // 获取博客详情
  if(method == 'GET' && req.path == '/api/blog/detail') {
    const result = getDetail(id);
    return result.then(data => {
      return new SuccessModel(data)
    })
  }

  // 新建一篇博客
  if(method == 'POST' && req.path == '/api/blog/new') {
    // 该接口先验证是否登录
    const loginCheckResult = loginCheck(req)
    if(loginCheckResult) {  // 未登录
      return loginCheckResult  // 返回一个Promise，不会执行下面的语句
    }

    req.body.author = req.session.username  // 假数据，待开发登录时再改成真实数据
    const result = newBlog(req.body)
    return result.then(data => {
      return new SuccessModel(data)
    })
  }

  // 更新一篇博客
  if(method == 'POST' && req.path == '/api/blog/update') {
    // 该接口先验证是否登录
    const loginCheckResult = loginCheck(req)
    if(loginCheckResult) {  // 未登录
      return loginCheckResult  // 返回一个Promise，不会执行下面的语句
    }
    const result = updateBlog(id, req.body)
    return result.then(val => {
      if(val) {
        return new SuccessModel()
      } else {
        return new ErrorModel("更新博客失败")
      }
    })
  }

  // 删除一篇博客
  if(method == 'POST' && req.path == '/api/blog/del') {
    // 该接口先验证是否登录
    const loginCheckResult = loginCheck(req)
    if(loginCheckResult) {  // 未登录
      return loginCheckResult  // 返回一个Promise，不会执行下面的语句
    }
    const author = req.session.username  // 假数据，待开发登录时再改成真实数据
    const result = delBlog(id, author);
    return result.then(val => {
      if(val) {
        return new SuccessModel();
      } else {
        return new ErrorModel("删除博客失败");
      }
    })
  }
}

module.exports = handleBlogRouter;